package com.carmaintain.servlets.customer;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.db.UserLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		Map<String, String> user;
		String userid = "";
		String password = "";
		String email = "";
		String city = "";
		
		boolean usernameFlag;
		boolean emailFlag;
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_SIGN_ERROR);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			
			if(Utils.empty(request.getParameter("customer_id")) || Utils.empty(request.getParameter("password")) 
					|| Utils.empty(request.getParameter("email"))){
				result.setCode(Constants.CODE_MISS_PARAMATER);
				result.setMessage(Constants.MESSAGE_MISS_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else{
				userid = request.getParameter("customer_id").trim();
				password = request.getParameter("password").trim();
				email = request.getParameter("email").trim();
				usernameFlag = UserLib.checkUserNameExist(userid, Constants.USER_TYPE_CUSTOMER);
				emailFlag = UserLib.checkEmailExist(email, Constants.USER_TYPE_CUSTOMER);
			}
			
			if (!Utils.empty(request.getParameter("city"))) {
				city = request.getParameter("city");
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		
		try {
			if(usernameFlag) {//用户名已存在
				result.setCode(Constants.CODE_USERNAME_EXIST);
				result.setMessage(Constants.MESSAGE_USERNAME_EXISIST);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else if(emailFlag){ //邮件地址已存在
				result.setCode(Constants.CODE_EMAIL_EXIST);
				result.setMessage(Constants.MESSAGE_EMAIL_EXISIST);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else {
				user = UserLib.customerRegister(userid, password, email, city);
				if (user == null) {
					result.setCode(Constants.CODE_EXCEPTION);
					result.setMessage(Constants.MESSAGE_EXCETION);
				} else {
					result.setCode(Constants.CODE_SUCCESS);
					result.setMessage(Constants.MESSAGE_SUCCESS);
					result.setResultData(user);
				}
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
